package pandora.bo.DCC;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import pandora.data.ICustomerDAO;
import pandora.data.IGenericSearchDAO;
import pandora.data.ISearchDAO;
import pandora.data.mysql.JDBCCustomerDAO;
import pandora.data.mysql.JDBCGenericSearchDAO;
import pandora.data.mysql.JDBCSearchDAO;
import pandora.dto.ProductDTO;
import pandora.dto.CustomerDTO;
import pandora.dto.TypeDTO;

public class CustomerStats implements IStat {
	private CustomerDTO customer;
	private IGenericSearchDAO genericSearchDAO = new JDBCGenericSearchDAO();
	private ISearchDAO searchDAO = new JDBCSearchDAO();
	private ICustomerDAO customerDAO = new JDBCCustomerDAO();
	
	public CustomerStats(String customerDNI){
		this.customer = customerDAO.getCustomerByDNI(customerDNI);
	}
	
	public List<ProductPar> mostSearchedProducts() {
		List<ProductPar> msp = new ArrayList<ProductPar>();
		
		List<ProductDTO> searchedProducts = searchDAO.selectAllSearchedCustomerProducts(customer.getCustomerDNI());
		
		for(ProductDTO p: searchedProducts){
			Integer c = searchDAO.countSearchedCustomerProduct(customer.getCustomerDNI(),p.getBarcode());
			ProductPar pp = new ProductPar(p,c);
			
			if(!msp.contains(pp))
				msp.add(pp);
		}
		
		Collections.sort(msp);
		Collections.reverse(msp);
		return msp;
	}

	public List<ProductPar> mostSearchedProductsBetween(Date d1, Date d2) {
		List<ProductPar> mspb = new ArrayList<ProductPar>();
		
		List<ProductDTO> pisb = searchDAO.selectAllSearchedCustomerProductsBetween(customer.getCustomerDNI(),d1, d2);
		
		for(ProductDTO p: pisb){
			Integer c = searchDAO.countSearchedCustomerProductBetween(customer.getCustomerDNI(),p.getBarcode(),d1,d2);
			ProductPar pp = new ProductPar(p,c);
			
			if(!mspb.contains(pp))
				mspb.add(pp);
		}
		
		Collections.sort(mspb);
		Collections.reverse(mspb);
		return mspb;
	}

	public List<TypePar> mostSearchedTypes() {
		List<TypePar> mst = new ArrayList<TypePar>();
		
		List<TypeDTO> typesInStore = genericSearchDAO.selectAllSearchedCustomerTypes(customer.getCustomerDNI());
		
		for(TypeDTO t: typesInStore){
			Integer c = genericSearchDAO.countSearchedCustomerType(customer.getCustomerDNI(),t);
			TypePar tp = new TypePar(t,c);
			
			if(!mst.contains(tp))
				mst.add(tp);			
		}
		
		Collections.sort(mst);
		Collections.reverse(mst);
		return mst;
	}

	public List<TypePar> mostSearchedTypesBetween(Date d1, Date d2) {
		List<TypePar> mstb = new ArrayList<TypePar>();
		
		List<TypeDTO> tisb = genericSearchDAO.selectAllSearchedCustomerTypesBetween(customer.getCustomerDNI(),d1,d2);
		
		for(TypeDTO t: tisb){
			Integer c = genericSearchDAO.countSearchedCustomerTypeBetween(customer.getCustomerDNI(),t,d1,d2);
			TypePar tp = new TypePar(t,c);
			
			if(!mstb.contains(tp))
				mstb.add(tp);
		}
		
		Collections.sort(mstb);
		Collections.reverse(mstb);
		return mstb;
	}

}
